﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using FineAdmin.Common;
using FineAdmin.IService;
using FineAdmin.Model;

namespace FineAdmin.Web.Controllers
{
    [HandlerLogin]
    public class BaseController : Controller
    {
        protected const string SuccessText = "操作成功！";
        protected const string ErrorText = "操作失败！";
        public IButtonService ButtonService { get; set; }
        public IOpRecordService OpRecordService { get; set; }

        public ISystemParameterService SystemParameterService { get; set; }

        public IAdZoneService AdZoneService { get; set; }
        public IMaterialGoodsService MaterialGoodsService { get; set; }

        protected override void OnException(ExceptionContext filterContext)
        {
            if (!filterContext.ExceptionHandled)
            {
                //string message = string.Format("消息类型：{0}\r\n消息内容：{1}\r\n引发异常的方法：{2}\r\n引发异常源：{3}"
                //, filterContext.Exception.GetType().Name
                //, filterContext.Exception.Message
                //, filterContext.Exception.TargetSite
                //, filterContext.Exception.Source + filterContext.Exception.StackTrace
                //);

                ////记录日志
                //log.Error(message);
                ////转向
                //filterContext.ExceptionHandled = true;
                Log.WriteFatal(filterContext.Exception);

                filterContext.Result = new RedirectResult("~/PageView/error/404.html");//跳转错误页，地址根据自己的改
            }
            base.OnException(filterContext);
        }

        public OperatorModel Operator
        {
            get { return OperatorProvider.Provider.GetCurrent(); }
        }
        // GET: Base
        public virtual ActionResult Index(int? id)
        {
            var _menuId = id == null ? 0 : id.Value;
            var _roleId = Operator.RoleId;
            if (id != null)
            {
                ViewData["RightButtonList"] = ButtonService.GetButtonListByRoleIdModuleId(_roleId, _menuId, PositionEnum.FormInside);
                ViewData["TopButtonList"] = ButtonService.GetButtonListByRoleIdModuleId(_roleId, _menuId, PositionEnum.FormRightTop);
            }
            return View();
        }
        /// <summary>
        /// 操作成功
        /// </summary>
        /// <param name="message"></param>
        /// <returns></returns>
        protected virtual AjaxResult SuccessTip(string message = SuccessText)
        {
            return new AjaxResult { state = ResultType.success.ToString(), message = message };
        }
        /// <summary>
        /// 操作失败
        /// </summary>
        /// <param name="message"></param>
        /// <returns></returns>
        protected virtual AjaxResult ErrorTip(string message = ErrorText)
        {
            return new AjaxResult { state = ResultType.error.ToString(), message = message };
        }

        public List<AdZoneModel> AdZoneAll()
        {
            return AdZoneService.GetAll().ToList();
        }

        public List<SystemParameterModel> SystemParameterAll() {

            return SystemParameterService.GetAll().ToList();
        }
    }
}